package com.mdoya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mdoya.domain.AddressBook;
import com.mdoya.mapper.AddressBookMapper;
import com.mdoya.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Scope("singleton")
@Transactional(propagation = Propagation.REQUIRED)
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public void save(AddressBook addressBook) {
        addressBookMapper.insert(addressBook);
    }

    @Override
    public void update(LambdaUpdateWrapper<AddressBook> wrapper) {
        addressBookMapper.update(null,wrapper);
    }

    @Override
    public Integer updateById(AddressBook addressBook) {
        return addressBookMapper.updateById(addressBook);
    }

    @Override
    public AddressBook getById(Long id) {
        return addressBookMapper.selectById(id);
    }

    @Override
    public AddressBook getOne(LambdaQueryWrapper<AddressBook> queryWrapper) {
        return addressBookMapper.selectOne(queryWrapper);
    }

    @Override
    public List<AddressBook> list(LambdaQueryWrapper<AddressBook> queryWrapper) {
        return addressBookMapper.selectList(queryWrapper);
    }

    /**
     * 根据用户删除对应地址簿
     *
     * @param ids
     * @param userId
     * @return
     */
    @Override
    public Integer deleteByid(Long ids, Long userId) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null,AddressBook::getUserId,userId);
        queryWrapper.eq(ids != null,AddressBook::getId,ids);
        int rows = addressBookMapper.delete(queryWrapper);
        return rows;
    }
}
